1215C - Swap Letters - CodeForces Solution


constructive algorithms greedy *1500

Please click on ads to support us..

Python Code:

n=int(input())s=input()
t=input()
l,r=[],[]
for i in range(n):
    if s[i]+t[i]=='ba':
        r.append(i+1)
    if s[i]+t[i]=='ab':
        l.append(i+1)
p=len(l)+len(r)
if p%2==0:
    if len(l)%2==0:
        print(p//2)
        r=r+l
        for i in range(0,p,2):
            print(r[i],r[i+1])
    else:
        print(p//2+1)
        print(r[0],r[0])
        print(l[0],r[0])
        l=l[1:]+r[1:]
        for i in range(0,p-2,2):
            print(l[i],l[i+1])
else:
    print(-1)

C++ Code:

#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll; 
typedef vector<ll> vl;
typedef set<ll> sl;
const int mod = (int)1e9 + 7;
#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)),  __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); } 
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl
/*
Code to set Precision
std::cout << std::fixed;
std::cout << std::setprecision(6);// value after decimal
std::cout << (double)vk[i] << std::endl;
*/
/* struct Graph
{
	vector<vector<int>> adj;
	Graph(int n)
	{
		adj.resize(n+1);
	}	
	void add_edge(int a, int b, bool directed = false)
	{
		adj[a].pb(b);
		if(!directed) adj[b].pb(a);
	}
}; */
void solve(){
    ll n;cin>>n;
    string a, b; cin>>a>>b;

    ll abyb=0, bbya=0;

    ll counta=0; ll countb=0;


    //dbg(a); dbg(b);
    set<ll> posabyb;
    set<ll> posbbya;

    for(ll i=0; i<n; i++){


        if(a[i]=='a'){counta++;}
        if(b[i]=='a'){counta++;}


        if(a[i]=='b'){countb++;}
        if(b[i]=='b'){countb++;}


        if(a[i]=='a'&&b[i]=='b'){
            abyb++;
            posabyb.insert(i+1);
        }

        if(a[i]=='b'&&b[i]=='a'){
            bbya++;
            posbbya.insert(i+1);
        }

    }
    //dbg(counta);dbg(countb);

    if(counta%2==1||countb%2==1){
        cout<<-1<<endl;return;
    }

    //dbg(posabyb);
    ll ans=(abyb/2)+bbya/2;
    if(abyb%2==1){ans+=2;}

    cout<<ans<<endl;
    

    
    while(posabyb.size()>1){
        auto it=posabyb.begin();
        auto last=--posabyb.end();
        cout<<*it<<" "<<*(last)<<endl;
        
        posabyb.erase(posabyb.begin());
        posabyb.erase(last);
    }
    while(posbbya.size()>1){
        auto it=posbbya.begin();
        auto last=--posbbya.end();
        cout<<*it<<" "<<*(last)<<endl;
        
        posbbya.erase(posbbya.begin());
        posbbya.erase(last);
    }
    

    auto final=posabyb.begin();
    auto notfinal= posbbya.begin();


    if(posabyb.size()!=0){
        cout<<*final<<" "<<*final<<endl;
        cout<<*final<<" "<<*notfinal<<endl;
    }
}

int main(){
    fast;
    ll t;t=1;while(t--){solve();}
    
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

519A - A and B and Chess
725B - Food on the Plane
154B - Colliders
127B - Canvas Frames
107B - Basketball Team
245A - System Administrator
698A - Vacations
1216B - Shooting
368B - Sereja and Suffixes
1665C - Tree Infection
1665D - GCD Guess
29A - Spit Problem
1097B - Petr and a Combination Lock
92A - Chips
1665B - Array Cloning Technique
1665A - GCD vs LCM
118D - Caesar's Legions
1598A - Computer Game
1605A - AM Deviation
1461A - String Generation
1585B - Array Eversion
1661C - Water the Trees
1459A - Red-Blue Shuffle
1661B - Getting Zero
1661A - Array Balancing
1649B - Game of Ball Passing
572A - Arrays
1455A - Strange Functions
1566B - MIN-MEX Cut
678C - Joty and Chocolate